<?php
/* ******************************
 *	letterA aJax actions
 * ******************************
 */

include('../../../includes/classes/core.php');
 

$action		= $_REQUEST['act'];
$error		= '';
$data		= '';
$user_id	= $_SESSION['USERID'];


switch ($action) {
	case 'get_edit_page':
		$wareHouseDetID	= $_REQUEST['id'];
		$wareHouse		= $_REQUEST['wareHouse'];
		$barcode		= $_REQUEST['barcode'];
		$page 			= GetPage($wareHouseDetID, $wareHouse, $barcode);
		$data			= array('page'	=> $page);

		break;
	case 'get_receive_page':
		$page 			= GetReceivePage();
		$data			= array('page'	=> $page);

		break;
	case 'disable' :
		disable_list($_REQUEST['id']);
		break;
    case 'get_list':
	    $count			= $_REQUEST['count'];
	    $hidden 		= $_REQUEST['hidden'];
	    $wareHouse_id	= $_REQUEST['id'];
	    $rResult = mysql_query("SELECT		DISTINCT
											`wd`.`id`,
											IF(wd.type=2,CONCAT(`wd`.`barcode`,' (საგარანტიო)'),`wd`.`barcode`),
							    			`barcodes`.serial_number,
											`production`.`name`,
											IF(ISNULL((SELECT  DISTINCT warehouse_producing.packer_id FROM warehouse_producing  WHERE warehouse_producing.warehouse_detail_id = wd.id AND NOT ISNULL(warehouse_producing.packer_id)   LIMIT 1)) ,
											IF(`wd`.`status` = 0, '<img src=\"media/images/delete1.png\">', '<img src=\"media/images/apply.png\">'),
																		'<img style=\"width: 15px; height: 15px;\" src=\"media/images/pack.png\">'
																	)
								FROM		 `warehouse_detail` AS wd
								LEFT JOIN 	`warehouse` ON `warehouse`.`id` = `wd`.`warehouse_id`
								LEFT JOIN 	`barcodes`  ON `barcodes`.`code`  = `wd`.`barcode`
								LEFT JOIN	`production` ON `production`.`id` = `wd`.`prod_id`
								WHERE		`wd`.`warehouse_id` = '$wareHouse_id' AND `wd`.`actived` = 1 ORDER BY wd.id") ;

		$data = array(
			"aaData"	=> array()
		);
		$j=1;
		while ( $aRow = mysql_fetch_array( $rResult ) )
		{
			$row = array();
			for ( $i = 0 ; $i < $count ; $i++ )
			{
				// General output

				$row[] = $aRow[$i];
				if($i == ($count-5)){$row[] = $j;}
				if($i == ($count - 1)){
					$row[] = '<input type="checkbox" name="check_' . $aRow[$hidden] . '" class="check" value="' . $aRow[$hidden] . '" />';
				}
			}
			$j++;
			$data['aaData'][] = $row;
		}

		break;
    case 'get_receive_list':
    	$count			= $_REQUEST['count'];
    	$hidden 		= $_REQUEST['hidden'];
    	$wareHouse_id	= $_REQUEST['id'];

    	//$rResult = mysql_query("");

    	$data = array(
    			"aaData"	=> array()
    	);
    	/*
    	while ( $aRow = mysql_fetch_array( $rResult ) )
    	{
    		$row = array();
    		for ( $i = 0 ; $i < $count ; $i++ )
    		{
    			// General output
    			$row[] = $aRow[$i];
    		}
    		$data['aaData'][] = $row;
    	}*/

    	break;
	case 'add_barcode':
		$wareHouse 		= $_REQUEST['wareHouse'];
		$barcode 		= $_REQUEST['barcode'];
		addBarcode($wareHouse,$barcode);






        break;
	case 'get_packin_list' :
		$count			= $_REQUEST['count'];
		$hidden 		= $_REQUEST['hidden'];

		$rResult = mysql_query("SELECT		cartridge_parts_type.id,
											cartridge_parts_type.`name`,
											cartridge_parts_type.checked
								FROM		cartridge_parts_type
								WHERE		cartridge_parts_type.category = 4 && cartridge_parts_type.actived = 1");
		$data = array(
				"aaData"	=> array()
		);
		while ( $aRow = mysql_fetch_array( $rResult ) )
		{
			$row = array();
			for ( $i = 0 ; $i < $count  ; $i++ )
			{
				// General output
				$row[] = $aRow[$i];
				if( $i == ($count - 1) ){
					if($aRow[2] == '1'){
						$row[] = '<input type="checkbox" name="check_' . $aRow[$hidden] . '" class="check" value="' . $aRow[$hidden] . '" checked/>';
					}else{
						$row[] = '<input type="checkbox" name="check_' . $aRow[$hidden] . '" class="check" value="' . $aRow[$hidden] . '" />';
					}
				}
			}
			$data['aaData'][] = $row;
		}

		break;
		case 'get_inner_packin_list' :
			$count			= $_REQUEST['count'];
			$hidden 		= $_REQUEST['hidden'];

			$rResult = mysql_query("SELECT	cartridge_parts_type.id,
											cartridge_parts_type.`name`,
											cartridge_parts_type.checked
								FROM		cartridge_parts_type
								WHERE		cartridge_parts_type.category = 3 && cartridge_parts_type.actived = 1");
			$data = array(
					"aaData"	=> array()
			);
			while ( $aRow = mysql_fetch_array( $rResult ) )
			{
				$row = array();
				for ( $i = 0 ; $i < $count  ; $i++ )
				{
					// General output
					$row[] = $aRow[$i];
					if( $i == ($count - 1) ){
						if($aRow[2] == '1'){
							$row[] = '<input type="checkbox" name="check_' . $aRow[$hidden] . '" class="check" value="' . $aRow[$hidden] . '" checked/>';
						}else{
							$row[] = '<input type="checkbox" name="check_' . $aRow[$hidden] . '" class="check" value="' . $aRow[$hidden] . '" />';
						}
					}
				}
				$data['aaData'][] = $row;
			}

			break;
		case 'add_cbarcode_page':
			$page 			= GetcBarcodePage();;
			$data			= array('page'	=> $page);

			break;
		case 'add_pbarcode_page':
			$page 			= GetpBarcodePage();;
			$data			= array('page'	=> $page);

			break;
    default:
       $error = 'Action is Null';
}

$data['error'] = $error;

echo json_encode($data);


function addBarcode($wareHouse,$barcode){
	$rsql = mysql_query("SELECT		`warehouse_detail`.`barcode`
						 FROM		`warehouse_detail`
						 WHERE		`warehouse_detail`.`barcode` = '".$barcode."' &&  `warehouse_id` = '$wareHouse' AND `warehouse_detail`.actived=1");


	$in_reserve_check   = mysql_fetch_array(mysql_query("SELECT `id`, `warehouse_detail_id` FROM `barcodes` WHERE `code` = '$barcode' AND `status` = 2"));
	$if_is_reserve      = mysql_fetch_array(mysql_query("SELECT `id`, a_letter_id FROM warehouse WHERE id='$wareHouse'"));
	if(is_null($if_is_reserve[1])){
		$if_is_reserve      = mysql_fetch_array(mysql_query("SELECT `id`, d_letter_id FROM warehouse WHERE id='$wareHouse'"));
	}
	$aRow = mysql_fetch_array($rsql);
	if($aRow == ''){
		$clientID = (int) substr($barcode, 1, 4);
		$cartridgeID = (int) substr($barcode, 5, 4);
		$rsql = mysql_query("SELECT			`warehouse_detail`.`id`,
											`warehouse_detail`.`prod_id`,
											`client_objects`.`client_id`
							 FROM 			`warehouse_detail`
							 LEFT JOIN		`warehouse`     ON `warehouse`.`id` = `warehouse_detail`.`warehouse_id`
							 LEFT JOIN 		`client_objects` ON `client_objects`.`id` = `warehouse`.`client_object_id`
							 WHERE 			`warehouse_detail`.`warehouse_id` = '$wareHouse' && `warehouse_detail`.`barcode` = ''");

		while ( $aRow = mysql_fetch_array( $rsql ) ){
			if($aRow['2'] == $clientID){
				if($aRow['1'] == $cartridgeID){
					mysql_query("UPDATE
									`warehouse_detail`
								SET
									`barcode`	= $barcode
								WHERE
									`id`		= $aRow[0]
								");
					mysql_query("UPDATE
									`warehouse_detail_history`
								SET
									`barcode`	= $barcode
								WHERE
									`id`		= $aRow[0]
					");



					if(is_null($if_is_reserve[1])){
							mysql_query("UPDATE
								`barcodes`
								SET
								`warehouse_detail_id` = $aRow[0],
								`status`	= 2
								WHERE
								`code`		= '$barcode'");
					}




					ChangeLocation($barcode);
					$error ='';

					if(!is_null($in_reserve_check[0])){
						global $error;
						$date = date('Y-m-d H:i:s');
						$warehouse_producing = mysql_query("SELECT   tester_id,
																	 person_id,
																	 packer_id,
																	 cartridge_part_id,
																	 printer_part_id,
																	 count,
																	 production_id
														       FROM  warehouse_producing
														      WHERE  warehouse_detail_id = $in_reserve_check[1]");



						while ($wProd = mysql_fetch_array($warehouse_producing)){

							for($i=0; $i<=6; $i++){
								if(is_null($wProd[$i])){
									$wProd[$i] = 'NULL';
								}
							}

							mysql_query("INSERT INTO warehouse_producing (tester_id, person_id, packer_id,
																         warehouse_detail_id, cartridge_part_id,
                                                                         printer_part_id, count, production_id,date)
											  VALUES ($wProd[0], $wProd[1], $wProd[2], $aRow[0],
												      $wProd[3], $wProd[4], $wProd[5], $wProd[6], '$date');
										");

						}

						mysql_query("UPDATE
						            `warehouse_detail`
								SET
								   `status`	= 1
								WHERE
								   `id`	= '$aRow[0]'
						");
						mysql_query("UPDATE
										`warehouse_detail_history`
									SET
										`status`	= 1
									WHERE
										`id`	= '$aRow[0]'
						");


						mysql_query("UPDATE
										      `barcodes`
										SET
										      `status`	= 1
									   WHERE
									          `code`	= '$barcode'
									 ");
					}

					break;
				}else{
					global $error;
					$error = 'მოდელი არასწორია!';
				}
			}else{
				global $error;
				$error = 'კლიენტი არასწორია!';
			}
		}
	}else{
		global $error;
		$error = 'შტრიხკოდი უკვე ჩამატებულია!';
	}
}


function ChangeLocation($barcode){
	$result = mysql_query("	SELECT `reserve`.`id`
			FROM `reserve`
			WHERE `reserve`.`barcode` = '$barcode'");

	$result = mysql_num_rows($result);

	if ( $result >= 1) {
		mysql_query("	UPDATE 	`reserve`
						SET 	`reserve`.`object_id` = '134',
								`reserve`.`production_condition` =  0
						WHERE	`reserve`.`barcode` = '$barcode' ");
	}else {
		$c_data = date('Y-m-d H:i:s');
		$prod_id 	= (int) substr($barcode, 5, 4);

		mysql_query("	INSERT INTO reserve
							(object_id, production_id, barcode, production_condition, quantity, create_date)
						VALUES
							('134', $prod_id, '$barcode', 0, 1, '$c_data')");
	}
}
function disable_list($warehouse_detail_id){
		mysql_query("	UPDATE 	warehouse_detail
						SET		warehouse_detail.actived = 0
						WHERE	warehouse_detail.id = $warehouse_detail_id");
		mysql_query("	UPDATE 	warehouse_detail_history
						SET		warehouse_detail_history.actived = 0
						WHERE	warehouse_detail_history.id = $warehouse_detail_id");

}
function GetCartridgeType()
{
	$data = '';
	$req = mysql_query("SELECT 	`id`,
    						   	`name`
						FROM 	`cartridge_type`");

	if($point == ''){
		$data = '<option value="0" selected="selected"></option>';
	}

	while( $res = mysql_fetch_assoc( $req )){
		$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
	}

	return $data;
}
function GetcBarcodePage()
{
	$data = '
	<div id="dialog-form">
	    <fieldset>
	    	<legend>ძირითადი ინფორმაცია</legend>

	    	<table class="dialog-form-table">
				<tr>
					<td style="width: 170px;"><label for="type">ტიპი</label></td>
					<td>
						<select id="type" class="idls">' . GetCartridgeType() . '</select>
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="barcode_client">კლიენტი</label></td>
					<td>
						<div class="seoy-row" id="barcode_client_seoy">
							<input type="text" id="barcode_client" class="idle seoy-large" onblur="this.className=\'idle seoy-large\'" onfocus="this.className=\'activeField seoy-large\'" value="" />
							<button id="client_btn" class="combobox">barcode_client</button>
						</div>
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="barcode_cartridge">კარტრიჯი</label></td>
					<td>
						<div class="seoy-row" id="barcode_cartridge_seoy">
							<input type="text" id="barcode_cartridge" class="idle seoy-large" onblur="this.className=\'idle seoy-large\'" onfocus="this.className=\'activeField seoy-large\'" value="" />
							<button id="barcode_cartridge_btn" class="combobox">barcode_cartridge</button>
						</div>
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="old_barcode">ძველი შტრიხკოდი</label></td>
					<td>
						<input type="text" id="old_barcode" class="idle seoy-large" onblur="this.className=\'idle seoy-large\'" onfocus="this.className=\'activeField seoy-large\'" value="' . $res['old_barcode'] . '" />
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="count">რაოდენობა</label></td>
					<td>
						<input type="text" id="count" class="idle num" onblur="this.className=\'idle num\'" onfocus="this.className=\'activeField num\'" value="1" />
					</td>
				</tr>
			</table>
			<!-- ID -->
			<input type="hidden" id="barcode_id" value="" />
        </fieldset>
    </div>
    ';
	return $data;
}

function GetpBarcodePage()
{
	$data = '
	<div id="dialog-form">
	    <fieldset>
	    	<legend>ძირითადი ინფორმაცია</legend>

	    	<table class="dialog-form-table">
				<tr>
					<td style="width: 170px;"><label for="barcode_client">კლიენტი</label></td>
					<td>
						<div class="seoy-row" id="barcode_client_seoy">
							<input type="text" id="barcode_client" class="idle seoy-large" onblur="this.className=\'idle seoy-large\'" onfocus="this.className=\'activeField seoy-large\'" value="' . $res['client'] . '" />
							<button id="client_btn" class="combobox">barcode_client</button>
						</div>
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="barcode_printer">პრინტერი</label></td>
					<td>
						<div class="seoy-row" id="barcode_printer_seoy">
							<input type="text" id="barcode_printer" class="idle seoy-large" onblur="this.className=\'idle seoy-large\'" onfocus="this.className=\'activeField seoy-large\'" value="' . $res['printer'] . '" />
							<button id="barcode_printer_btn" class="combobox">barcode_printer</button>
						</div>
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="serial_number">სერიული ნომერი</label></td>
					<td>
							<input type="text" id="serial_number" class="idle seoy-large" onblur="this.className=\'idle seoy-large\'" onfocus="this.className=\'activeField seoy-large\'" value="' . $res['serial_number'] . '" />
					</td>
				</tr>
			</table>
			<!-- ID -->
			<input type="hidden" id="barcode_id" value="" />
        </fieldset>
    </div>';
	return $data;
}

function GetPage($wareHouseDetID, $wareHouse, $barcode){
	if( empty($wareHouseDetID) ){
		$result = mysql_fetch_assoc( mysql_query("	SELECT	`warehouse_detail`.`id` AS `wareHouseDetID`
													FROM	`warehouse_detail`
													WHERE	`warehouse_detail`.`warehouse_id` = '$wareHouse' AND `warehouse_detail`.`barcode` = '$barcode'"));
		$wareHouseDetID	= $result['wareHouseDetID'];
		if( empty($wareHouseDetID) ){
			GLOBAL $error;
			$error = 'შტრიხკოდი არასწორია';
		}

	}

	$res =  mysql_fetch_assoc(mysql_query("SELECT     `warehouse_detail`.`barcode` AS `barcode`,
											          `production`.`name` AS `name`
											FROM	  `warehouse_detail`
											LEFT JOIN  production ON production.id = warehouse_detail.prod_id
											WHERE	  `warehouse_detail`.`id` = $wareHouseDetID"));
	$data = '
	<div id="dialog-form">
		<fieldset>
			<legend>შეფუთვა</legend>
			<div class="inner-table">
				<table>
					   <tr>
				          <td>დასახელება:&nbsp&nbsp</td>
						  <td style="font-weight:bold;">'.$res['name'].'</td>
				       </tr>
					   <tr>
				          <td style="padding-top:10px;">შტრიხკოდი:&nbsp&nbsp</td>
						  <td style="font-weight:bold; padding-top:10px;">'.$res['barcode'].'</td>
				       </tr>
				</table>
				    <div id="dt_example" class="ex_highlight_row">
				        <div id="container" class="overhead_container">
				        	<div id="button_area">
				        	</div>
				            <div id="dynamic">
								<table class="display" id="inner_packing_list">
					                    <thead>
					                        <tr id="datatable_header">
					                            <th>ID</th>
					                            <th style="width: 100%">ტიპი</th>
					                            <th style="width: 80px">#</th>
					                        </tr>
					                    </thead>
					                    <thead>
					                        <tr class="search_header">
					                            <th class="colum_hidden">
					                            	<input type="text" name="search_idee" value="ფილტრი" class="search_init" />
					                            </th>
					                            <th>
					                                <input type="text" name="search_typee" value="ფილტრი" class="search_init" />
					                            </th>
					                            <th>
					                            </th>
					                        </tr>
					                    </thead>
				                </table>
				            </div>
				        </div>
				    </div>
				</div>
		</fieldset>
    </div>
	<div>
		<input type="hidden" style="" name="search_id" class="search_init"  id="wareHouseDetailID_packing" value="'.$wareHouseDetID.'"/>
	</div>';

	return $data;
}

function Getlocation(){
	$data = '';
	$req = mysql_query("SELECT	`id`,
								`name`
						FROM	`object`
						WHERE	`object`.`type` = 2");
	while( $res = mysql_fetch_assoc($req)){
		if( $res['id'] == '133' ){
			$data .= '<option value="' . $res['id'] . '" selected>' . $res['name'] . '</option>';
		}else{
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}
	return $data;
}

function GetStatus(){
	$data ='';
	$array = array(
		0 => 'დასამუხტი',
		1 => 'დამუხტული'
	);
	for($i = 0; $i < 2; $i++){
			$data .= '<option value="' . $i . '" >' . $array[$i] . '</option>';
	}
	return $data;
}

function GetReceivePage(){
	$data = '
		<div id="dialog-form">
		    <fieldset>
		    	<legend>კატრიჯების მიღება</legend>
				<table border="0" width="100%">
				<tr>
					<th>
						<label for="location">მდებარეობა :</label>
					</th>
					<th>
						<select id="location" class="idls address"  style=" float: right;">' . Getlocation() . '</select>
					</th>
				</tr>
				<tr>
					<th>
						<div  style="margin-top: 12px;"><label for="status ">მდგომარეობა :</label></div>
					</th>
					<th>
						<select id="status" class="idls address"  style=" float: right; margin-top: 10px;">' . GetStatus() . '</select>
					</th>
				</tr>
				<tr >
					<th>
						<div  style="margin-top: 12px;"><label for="add_barcode_input1 ">შტრიხკოდი :</label></div>
					</th>
					<th>
						<input style="display:inline; margin-top: 10px; float: right; width: 217px;" id="add_barcode_input1" type="text" maxlength="15"/>
					</th>
				</tr>
			</table>
        </fieldset>
		<!-- ID -->
    </div>


			';
	return $data;
}
?>
